package com.longtop.perform.khyg.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.security.BaseUncheckedException;
import com.longtop.framework.sys.file.context.FileContext;
import com.longtop.framework.sys.file.supports.UpLoadedFiles;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.StringUtil;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;
import com.longtop.intelliweb.view.Page;
import com.longtop.intelliweb.view.Record;
import com.longtop.perform.khyg.model.TKhygIndex;
import com.longtop.perform.khyg.service.ProjectService;
import com.longtop.perform.khyg.service.TaskService;

/**
 * 
 * @author 孙景弘
 * 
 */
public class TaskAction extends BaseAction {

	private TaskService service;
	private final String DATASET_PORJECT_INDEX = "Dataset_project_index";
	private final String DATASET_PROJECT_SEGMENT = "Dataset_project_segment";
	private final String DATASET_TASK_USER = "Dataset_task_user";
	private final String DATASET_PROJECT = "Dataset_project";

	// 查询条件：
	Map<String, Object> queryMap = new HashMap<String, Object>();

	public TaskAction() {
		this.service = this.getBean(TaskService.BEAN_ID);
	}

	/**
	 * 系统共用：初始化调用
	 */
	public void init(IInitContext context) {
		super.init(context);
	}

	/**
	 * 本页jsp：初始化业务数据(覆写父类方法)
	 */
	public void thisPageInit(IInitContext context) {

		commonLoadData(context);

		String task = this.getParameter(context, "task");
		String segmentId = this.getParameter(context, "segmentId");
		if (!StringUtil.isEmpty(segmentId)) {
			Dataset dataset_task_user = this.getDataset(context, DATASET_TASK_USER);
			// 员工任务批量导入：
			String uploadbean = this.getParameter(context, "uploadbean");
			if (!StringUtil.isEmpty(uploadbean)) {
				Map<String, Object> map = FileContext.getFileContext().getContext();
				UpLoadedFiles upLoadedFiles = (UpLoadedFiles) map.get("upLoadedFiles");// 取得上传的文件
				if (upLoadedFiles != null) {
					Page page = (Page) context.getControl("ePage");
					List<Map> userTaskMapList = upLoadedFiles.getExcelObjectList();
					try {
						service.getUserTaskFromImport(segmentId, dataset_task_user, userTaskMapList);
						page.setTag("importSuccess");// 给页面传递信息。
					} catch (Exception e) {
						this.hintErrorMessage(e.getMessage());
						page.setTag("importFail");
						e.printStackTrace();
					}
				}
			} else {
				if ("view".equals(task)) {// 员工任务查看
					service.getUserTaskView(segmentId, dataset_task_user, null);
				} else if ("track".equals(task)) {// 员工任务跟踪
					service.getUserTaskTrack(segmentId, dataset_task_user);
				}
			}
		}
	}

	/**
	 * 加载页面数据
	 * 
	 * @param context
	 */
	private void commonLoadData(IInitContext context) {
		Dataset dataset_project_segment = this.getDataset(context, DATASET_PROJECT_SEGMENT);
		Dataset dataset_project_index = this.getDataset(context, DATASET_PORJECT_INDEX);
		Dataset dataset_task_user = this.getDataset(context, DATASET_TASK_USER);
		if (!WebUtil.validateDatasetExist(this, dataset_project_segment, DATASET_PROJECT_SEGMENT) || !WebUtil.validateDatasetExist(this, dataset_project_index, DATASET_PORJECT_INDEX) || !WebUtil.validateDatasetExist(this, dataset_task_user, DATASET_TASK_USER))
			return;

		String projectId = this.getParameter(context, "projectId");

		// 方案对应指标：
		queryMap.put("projectId", projectId);
		queryMap.put("indexTypeNotEqual", "1");// 1代表定性指标，排除定性指标
		service.queryForPageByIbatis("khygProjectSql.projectIndexIdToName", null, queryMap, dataset_project_index, false);

		// 获取考核方案对应分段
		ProjectService projectService = this.getBean(ProjectService.BEAN_ID);
		List list = projectService.getProjectSegment(projectId);
		BeanUtil.listToDataset(list, dataset_project_segment, false);

		// 组装员工-指标任务明细DATASET：
		String task = this.getParameter(context, "task");
		service.bulidUserDLIndexDataset(dataset_project_index, dataset_task_user, task);

	}

	/**
	 * 获取考核方案Id对应名称
	 * 
	 * @param context
	 */
	public void queryProjectIdToName(IActionContext context) {
		Dataset dataset_project = this.getDataset(context, DATASET_PROJECT);
		if (!WebUtil.validateDatasetExist(this, dataset_project, DATASET_PROJECT))
			return;
		service.queryForPageByIbatis("khygProjectSql.projectIdToName", null, null, dataset_project, false);
	}

	/**
	 * 获取考核方案对应分段
	 * 
	 * @param context
	 */
	public void queryProjectSegment(IActionContext context) {
		Dataset dataset_project_segment = this.getDataset(context, DATASET_PROJECT_SEGMENT);
		String projectId = this.getParameter(context, "projectId");

		ProjectService projectService = this.getBean(ProjectService.BEAN_ID);
		List list = projectService.getProjectSegment(projectId);
		BeanUtil.listToDataset(list, dataset_project_segment, false);
	}

	/**
	 * 获取考核任务对应的员工明细
	 * 
	 * @param context
	 */
	public void queryTaskUser(IActionContext context) {
		Dataset dataset_task_user = this.getDataset(context, DATASET_TASK_USER);
		String segmentId = this.getParameter(context, "segmentId");

		service.getUserTaskView(segmentId, dataset_task_user, context);
	}

	/**
	 * 提交保存任务员工明细
	 * 
	 * @param context
	 */
	public void submitTaskUser(IActionContext context) {
		Dataset dataset_task_user = this.getDataset(context, DATASET_TASK_USER);
		Dataset dataset_project_index = this.getDataset(context, DATASET_PORJECT_INDEX);
		String submitType = this.getParameter(context, "submitType");// 提交的数据是更新的数据还是所有数据
		if ("all".equals(submitType)) {// 来自员工任务批量导入
			if (!WebUtil.validateDatasetExist(this, dataset_task_user, DATASET_TASK_USER))
				return;
		} else {
			if (!WebUtil.validateDatasetChange(this, dataset_task_user, DATASET_TASK_USER))
				return;
		}

		try {
			List<TKhygIndex> indexList = BeanUtil.datasetToList(dataset_project_index, TKhygIndex.class);
			List<Record> taskUserRecordList = dataset_task_user.getRecordList();

			service.saveUserTaskMx(indexList, taskUserRecordList);

			this.hintInfoMessage("保存成功。");
		} catch (Exception e) {
			this.hintErrorMessage("保存失败。");
			e.printStackTrace();
		}
	}

	/**
	 * 导出员工任务查看Excel
	 * 
	 * @param context
	 */
	public void exportTaskView(IActionContext context) {
		Dataset dataset_project_index = this.getDataset(context, DATASET_PORJECT_INDEX);
		String segmentId = this.getParameter(context, "segmentId");
		String isEmptyDataStr = this.getParameter(context, "isEmptyData");
		boolean isEmptyData = "true".equals(isEmptyDataStr) ? true : false;
		try {
			service.exportTaskView(dataset_project_index, segmentId, isEmptyData, context);
		} catch (BaseUncheckedException e) {
			this.hintErrorMessage(e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			this.hintErrorMessage("导出Excel失败。");
			e.printStackTrace();
		}
	}

	/**
	 * 导出员工任务跟踪Excel
	 * 
	 * @param context
	 */
	public void exportTaskTrack(IActionContext context) {
		Dataset dataset_project_index = this.getDataset(context, DATASET_PORJECT_INDEX);
		String segmentId = this.getParameter(context, "segmentId");
		try {
			service.exportTaskTrack(dataset_project_index, segmentId, context);
		} catch (BaseUncheckedException e) {
			this.hintErrorMessage(e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			this.hintErrorMessage("导出Excel失败。");
			e.printStackTrace();
		}
	}
}
